import BasePresenter from "../../../../../../redare/presenter/BasePresenter";
import * as MenuHttp from "../../../http/component/core/MenuHttp";

/**
 * Created by RdevPlugins.JsPresenterBuilderAction on 2020-03-07 13:02:40.
 */

export default class MenuPresenter extends BasePresenter {

    /**
     * @param body: {menuId, platformMenuId, parentMenuId, enabled, refPage, excRoot, sortToTree, levelTreeQuery, excMenuAndChildrenByMenuId, platform}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findMenuPage(body, query, success, fails) {

        if (this._onBeforeFindMenuPage) {
            let data = this._onBeforeFindMenuPage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        MenuHttp.findMenuPage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindMenuPageSuccess) {
                    result = this._onBeforeFindMenuPageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindMenuPageSuccess) {
                    this.view._onFindMenuPageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {menuId, platformMenuId, parentMenuId, enabled, refPage, excRoot, sortToTree, levelTreeQuery, excMenuAndChildrenByMenuId, platform}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findMenuList(body, query, success, fails) {

        if (this._onBeforeFindMenuList) {
            let data = this._onBeforeFindMenuList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        MenuHttp.findMenuList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindMenuListSuccess) {
                    result = this._onBeforeFindMenuListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindMenuListSuccess) {
                    this.view._onFindMenuListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

}
